package org.wang.gift.feign.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class DefaultOAuth2FeignRequestInterceptor implements RequestInterceptor {

    private static final String TOKEN_HEADER = "Authorization";
    @Override
    public void apply(RequestTemplate requestTemplate) {

        // 获取请求对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        //从请求头中获取token
        String token = request.getHeader(TOKEN_HEADER);

        //把token携带到写一个服务
        if (!StringUtils.isEmpty(token))
             requestTemplate.header(TOKEN_HEADER,token);

    }
}